package pers.vinsonws.jianghu.clientwin.ui;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.service.ApplyChatRoomService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther vinsonws
 * @date 2018/11/17 16:49
 */
public class ApplyListUi extends JFrame {
    private static final long serialVersionUID = 6L;
    private JPanel mainPanel;
    private JPanel listPanel;
    private String chatRoomName;
    private List<Integer> integerList = new ArrayList<>();

    public ApplyListUi( String chatRoomName) throws HeadlessException {
        mainPanel = new JPanel();
        mainPanel.setOpaque(false);
        listPanel = new JPanel();
        listPanel.setOpaque(false);
        JScrollPane jscrollPane = new JScrollPane(listPanel);
        jscrollPane.setBounds(13, 360, 900, 200);
        jscrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        mainPanel.add(jscrollPane);
        this.chatRoomName = chatRoomName;
        //Info.tabbedPane.addTab(chatRoomName + "的申请列表", mainPanel);
    }

    public void adimOperateApply(int userId, int chatRoomId){
        for (int i:integerList){
            if (i==userId) return;
        }
        JPanel one = new JPanel();
        one.setBackground(Color.lightGray);

        JLabel jLabel = new JLabel();
        jLabel.setText("用户id为：" + String.valueOf(userId));
        JButton pass = new JButton("同意");
        pass.setContentAreaFilled(false);

        pass.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ApplyChatRoomService.sendAdminOpreate(true, userId, chatRoomId);
                listPanel.remove(one);
                mainPanel.revalidate();
            }
        });
        JButton refuse = new JButton("拒绝");
        refuse.setContentAreaFilled(false);

        refuse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ApplyChatRoomService.sendAdminOpreate(false, userId, chatRoomId);
                listPanel.remove(one);
                mainPanel.revalidate();
            }
        });
        one.add(jLabel);
        one.add(pass);
        one.add(refuse);
        listPanel.add(one);

        //关闭按钮
        JButton btn = new JButton("X");
        JLabel lab = new JLabel(chatRoomName + "的申请列表");
        JTabbedPane pane = Info.indexUi.getTabbedPane();
        JPanel jp=new JPanel();
//        GridLayout gl= new GridLayout(1,1,10,0);
//        jp.setLayout(gl);
        lab.setHorizontalAlignment(JLabel.LEFT);//设置文字显示在最左边

        btn.setHorizontalAlignment(JLabel.RIGHT);// 设置文字显示在最右
        lab.setOpaque(false);
        //btn.setBounds(0, 0, 10, 10);
        btn.setContentAreaFilled(false);
        btn.setFont(new Font("宋体", Font.PLAIN, 12));
        btn.setMargin(new Insets(0, 0, 0, 0));
        btn.setBorder(null);
        jp.add(lab);
        jp.add(btn);
        pane.addTab(chatRoomName,mainPanel);
        pane.setTabComponentAt(pane.indexOfComponent(mainPanel),jp);

        //Info.indexUi.getTabbedPane().addTab(name, mainPanel);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                pane.remove(mainPanel);
                Info.tabbedPane.remove(Info.adminOpreate.get(chatRoomId));
                Info.adminOpreate.remove(chatRoomId);
                dispose();
            }
        });
    }
}
